package com.cheetah.start.module.monitor.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONObject;
import com.cheetah.start.module.monitor.service.ISysMsgService;
import com.cheetah.start.module.system.domain.SysSet;
import com.cheetah.start.module.system.service.ISysSetService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.HashMap;

@Slf4j
@Service
@RequiredArgsConstructor
public class SysMsgDingDingServiceImpl implements ISysMsgService {

    private final ISysSetService sysSetService;

    @Override
    public void send(String msg) {
        SysSet dingUrl = sysSetService.getByKey("dingding", false);
        String setValue = dingUrl.getSetValue();
        if (StrUtil.isNotBlank(setValue)) {
            HashMap<String, Object> res = new HashMap<>();
            HashMap<String, Object> content = new HashMap<>();

            content.put("content", "项目:cheetah快速开发系统\n" + "描述:" + msg);
            res.put("msgtype", "text");
            res.put("text", content);
            String body = HttpRequest.post(setValue)
                    .header(Header.CONTENT_TYPE, "application/json")
                    .timeout(5000)
                    .body(JSONObject.toJSONString(res)).execute().body();
            JSONObject result = JSONObject.parseObject(body);
            log.info("结果:[{}]", result);
            int errcode = result.getIntValue("errcode");
            if (errcode != 0) {
                log.warn("钉钉机器人通知失败:[{}]", body);
            }
        }
    }



}
